package com.sz.ruichar.utils.system;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;

/**
 * 日期格式
 */
public class DateUtil {

    /**
     * 获取当前时间，格式yyyyMMdd
     * @return
     */
    public static String getCurDay(){

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取前一天日期，格式yyyyMMdd
     * @return  yesterday
     */
    public static String getYesterday(){
        Date currentTime = new Date();
        Date yesterday = DateUtils.addDays( currentTime, -1 );      //结束日期向后顺延一天，用于mysql查询 [] ,ps：mysql查询日期范围是[)
        String yesterdayStr = date2Str( yesterday, "yyyyMMdd" );

        return yesterdayStr;
    }


    /**
     * 获取当前时间，格式yyyyMM
     * @return
     */
    public static String getCurMonth(){

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
        String dateString = formatter.format(currentTime);
        return dateString;
    }


    /**
     * 获取当前时间，格式dd，如28
     * @return
     */
    public static String getCurDayInMonth(){

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 字符串转时间
     * @param str       时间字符串，如 2017-04-15 19:00:00、2017-04-15
     * @param format    格式，如yyyy-MM-dd HH:mm:ss、yyyy-MM-dd
     * @return
     */
    public static Date str2Date(String str, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d = null;
        try {
            d = sdf.parse(str);
        } catch (ParseException e) {
        }
        return d;
    }

    /**
     * 时间转字符串
     * @param date      时间
     * @param format    格式，如yyyy-MM-dd HH:mm:ss、yyyy-MM-dd
     * @return
     */
    public static String date2Str(Date date, String format){

        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * Date对象转LocalDateTime对象
     * @param date      时间
     * @return
     */
    public static LocalDateTime date2LocalDateTime(Date date){

        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    /**
     * Date对象转LocalDateTime对象
     * @param localDateTime      时间
     * @return
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime){

        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);

        Date date = Date.from(zdt.toInstant());

        System.out.println("LocalDateTime = " + localDateTime);
        System.out.println("Date = " + date);
        return date;
    }

    /**
     * 时间差(分)==不计算年月日
     * @param  
     * @return
     */
    public static int getTimeIntervalExcludeYYMMDD(Date time1,Date time2)
    {
    	if(time1 == null || time2 ==null)
    	{
    		return -1;
    	}
    	else
    	{
    		Calendar cal = Calendar.getInstance();
    		cal.setTime(time1);
    		int hour1 = cal.get(Calendar.HOUR_OF_DAY);
    		
    		int minute1 = cal.get(Calendar.MINUTE);
    		
    		cal.setTime(time2);
    		int hour2 = cal.get(Calendar.HOUR_OF_DAY);
    		
    		int minute2 = cal.get(Calendar.MINUTE);
    		
    		int timeInterval = (hour1-hour2)*60 + minute1-minute2;
    		return Math.abs(timeInterval);
    	}
    }
    
    
    /**
     * 时间差(秒)
     * @param  
     * @return
     */
    public static Long getTimeIntervalCommon(Date time1,Date time2)
    {
    	if(time1 == null || time2 ==null)
    	{
    		return -1L;
    	}
    	else
    	{
    		long timeInterval = (time1.getTime() - time2.getTime())/1000;
    		return Math.abs(timeInterval);
    	}
    }

    
    
    public static void main(String args[])
    {
    	Calendar cal = Calendar.getInstance();
    	cal.set(2017, 10, 14, 18, 00);
    	Date d1 = cal.getTime();
    	cal.set(2017, 10, 15, 18, 0);
		Date d2 = cal.getTime();
		System.out.println("d1="+d1+"\nd2="+d2);
		System.out.println(getTimeIntervalExcludeYYMMDD(d1, d2));
    }
}

